"use strict";
const config = require("../../utils/config");
const QueueHelper = require("../../utils/queuehelper");
const bidemodel = require("../../model/bidemodel");
const UserLogic = require("../userlogic");
const RoomLogic = require("../roomlogic");
const FenhongCatLogic = require("../fenhongcatlogic");

module.exports = class bidelogic {
	
	static async loadbide(db,id)
	{
		var bide = new bidemodel(db,id);
		//先装载
		await bide.load();
		return bide;
	}

	// 增加
	static async addcount(db,id,keyname,count)
	{
		console.log("addcount",{id,keyname,count});
		const UserLogic = require("../userlogic");
		var bide = await this.loadbide(db,id);
		if (bide._id == 0)
		{
			bide._id = id;
		}
		bide[keyname] += count;
		var condition = await config.get("bide");
		var v = await config.get("bideprofit",keyname);
		var user = await UserLogic.loaduser(db,id);

		// 旅行团收益
		if (bide[keyname] >= condition[keyname])
		{
			var c = Math.floor(bide[keyname] / condition[keyname]);
			var k = 'leiji'+keyname;
			if (bide[k]<=25) {
				user.shangbide(v * c);
			}
			bide[keyname] = bide[keyname]%condition[keyname];
			
			bide[k] += v;
		}

		await bide.save();
		var u = await user.toparam();
		if (u['bide'] >= 100 && u['bide'] < 200)
		{
			var pos = await RoomLogic.findpos(db,id,user);
			if(!pos)
			{
				//延时
				await QueueHelper.putBean(db,"cat","permanent",{uid:id},10);
			}
			else
			{
				var ok = await RoomLogic.setpos(db,id,pos,1038);
				await user.load();
				var fenhongdog = await FenhongCatLogic.loadfenhongdog(db,id);
				await fenhongdog.shangdog();
				await user.shangbide(100000000);
			}
		}
		await user.save();
	}

	static async addcity(db,id,v)
	{
		var bide = await this.loadbide(db,id);
		if (bide._id == 0)
		{
			bide._id = id;
		}
		bide.leijiunlockcity += v;
		await bide.save();
	}
	
}